package com.example.gustavo.hello.utils;

import android.app.PendingIntent;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.telephony.SmsManager;
import android.text.TextUtils;

import java.util.ArrayList;


public class SmsUtil {

    private static final String SMS_URI_ALL =  "content://sms/";
    private static final String SMS_URI_INBOX = "content://sms/inbox";//收件箱
    private static final String SMS_URI_OUTBOX = "content://sms/outbox";//发件箱
    private static final String SMS_URI_SENT = "content://sms/sent";//已发送
    private static final String SMS_URI_DRAFT = "content://sms/draft";//草稿箱
    private static final String SMS_URI_FAILED = "content://sms/failed";//发送失败
    private static final String SMS_URI_QUEUED = "content://sms/queued";//待发送列表
    private static final String SMS_ACTION_SEND = "com.example.gustavo.hello.sent";//发送状态receiver
    private static final String SMS_ACTION_RECEIVER = "com.example.gustavo.hello.received";//接收状态receiver
    private static final String TAG = "SmsUtil";
    private static int requestCode;


    /**
     * 根据电话号码返回 联系人名称
     * @param context
     * @param phoneNum
     * @return
     */
    public static String getContactNameFromPhoneNum(Context context,String phoneNum){
        String contactName = "";
        ContentResolver contentResolver = context.getContentResolver();
        Cursor query = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                ContactsContract.CommonDataKinds.Phone.NUMBER + " = ?",
                new String[]{phoneNum}, null);
        if(query.moveToFirst()){
            contactName = query.getString(query.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            query.close();
        }
        return contactName;

    }

    /**
     * 删除内容为body的信息
     * @param context
     * @param body
     * @return
     */
    public static int delMessageWithBody(Context context, String body){
        return context.getContentResolver().delete(Uri.parse(SMS_URI_ALL), "body = ?", new String[]{body});
    }


    public static void sendSMS(Context context,String phone,String msg){

        if(!TextUtils.isEmpty(phone) && !TextUtils.isEmpty(msg)){

            if(MyPattern.isPhoneNumber( phone)){

                requestCode++;
                SmsManager sms = SmsManager.getDefault();
                Intent intent1 = new Intent();
                intent1.putExtra("phone", phone);
                intent1.setAction(SMS_ACTION_SEND);
                PendingIntent sendIntent = PendingIntent.getBroadcast(context, requestCode, intent1, PendingIntent.FLAG_UPDATE_CURRENT);

                Intent intent = new Intent();
                intent.putExtra("phone", phone);
                intent.setAction(SMS_ACTION_RECEIVER);
                PendingIntent receiverIntent = PendingIntent.getBroadcast(context, requestCode,intent,PendingIntent.FLAG_UPDATE_CURRENT);

                ArrayList<String> list = sms.divideMessage(msg);
                for (String s : list) {

                    sms.sendTextMessage(phone, null, s, sendIntent, receiverIntent);
//            delMessageWithBody(context, s);//发送之后删除信息 ,5.1安全问题删不掉

                }
                System.out.println("send phone = " + phone);
            }
        }

    }

}
